package com.controller;

import com.dao.BGoodDao;
import com.dao.BTypeDao;
import com.model.BGood;
import com.model.BType;
import com.model.Page;
import com.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class GoodController {
    @Resource
    Result result;

    @Resource
    BGoodDao bGoodDao;

    @Resource
    BTypeDao bTypeDao;

    private Result setResult(int status, String msg){
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
    //获取商品列表
    @GetMapping(value = "/goodList")
    public String goodList(ModelMap model, Page page){
        List<BGood> goodList= bGoodDao.list(page);
        int total =bGoodDao.total(page);
        page.caculateLast(total);
        model.addAttribute("goodList",goodList);
        model.addAttribute("page",page);
        model.addAttribute("total",total);
        return "backend/goods-list";
    }

    //添加商品信息
    @PostMapping(value = "/addGood")
    @ResponseBody
    public Result Add(BGood bGood, ModelMap model){
        try{
            int row =bGoodDao.insertGood(bGood);
            if(row>0){
                return this.setResult(1,"添加商品成功！");
            }else {
                return this.setResult(2,"添加商品出错，请联系管理员！");
            }
        }catch (Exception ex){
            return  this.setResult(0,ex.getMessage());
        }
    }
    @GetMapping(value = "/addGoodView")
    public String AddView(ModelMap model){
        List<BType> typeList=bTypeDao.typeList();
        model.addAttribute("typeList",typeList);
        return "backend/goods-add";
    }

    //删除用户
    @PostMapping(value = "/delGood")
    @ResponseBody
    public Result delAdmin(Integer id, ModelMap model){
        try{
            BGood bGood=bGoodDao.getGoodById(id);
            if(bGood==null){
                return this.setResult(0,"用户不存在！");
            }else {
                int row = bGoodDao.deleteGood(id);
                if (row > 0) {
                    return this.setResult(1, "删除用户成功！");
                } else {
                    return this.setResult(0, "删除用户出错，请联系管理员！");
                }
            }
        }catch (Exception ex){
            return this.setResult(0,"删除用户出错，请联系管理员！");
        }
    }

    //修改商品信息页面
    @GetMapping(value = "/updateGoodView")
    public  String updateGoodView(int id, ModelMap model){
        BGood bGood1 = bGoodDao.getGoodById(id);
        List<BType> typeList=bTypeDao.typeList();
        model.addAttribute("typeList",typeList);
        model.addAttribute("BGood",bGood1);
        return "backend/goods-edit";
    }
    //修改商品信息
    @PostMapping(value = "/updateGood")
    @ResponseBody
    public Result updateAdmin(BGood bGood, ModelMap model){
        try {
            int row =bGoodDao.updateGood(bGood);
            if(row>0){
                return  this.setResult(1,"修改商品成功！");
            }else {
                return  this.setResult(0,"修改商品出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }

    //视频详情页面的显示
    @GetMapping(value = "/detailView")
    public String detailView(int id, ModelMap model){
        BGood bGood=bGoodDao.getGoodById(id);
        List<BType> typeList=bTypeDao.typeList();
        model.addAttribute("bGood",bGood);
        model.addAttribute("typeList",typeList);
        return "front/detail";
    }

}
